/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL 
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.api.impl;

import com.db4o.nb.api.QueryData;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Holds the list of queries defined on the db4o database connection. 
 * @author klevgert
 */
public class QueryList extends ArrayList {

  /** used xml dom element */
  private Element element;
  
  /** 
   * Creates a new instance of QueryList 
   * @param element   xml dom element. 
   */
  public QueryList(Element element) {
    
    // store xml element.
    this.element = element;

    // Iterate the child nodes of the xml element
    // and create a temporary list of all query child objects.
    List tempList = new ArrayList();
    
    Node n = element.getFirstChild();
    while( n != null ) {
      String s = n.getNodeName();
      
      if( n.getNodeType() == Node.ELEMENT_NODE ) {
        Element e = (Element)n;
        
        // Queries must have an attribute kind
        if( e.hasAttribute(Constants.ATTR_KIND) ) {

          // get the kind value
          String kind = e.getAttribute(Constants.ATTR_KIND);

          // create query depeding on ist kind
          if( kind.equals(Constants.ATTR_KIND_SODA) ) {

            QueryData q = new SodaQueryImpl(e);
            tempList.add(q);

          } else if( kind.equals(Constants.ATTR_KIND_SQL) ) {

            QueryData q = new SqlQueryImpl(e);
            tempList.add(q);
          }
        }
      }
      
      n = n.getNextSibling();
    }

    // Now iterate the temporary list in a second step to add them to this list.
    Iterator it = tempList.iterator();
    while( it.hasNext() ) {
      QueryData q = (QueryData)it.next();
      this.add(q);
    }
  }
  
  /**
   * Adds a query object at a certain index.
   * @param index   insertion point
   * @param obj     query data object. 
   */
  public void add(int index, Object obj) {
    super.add(index, obj);

    AbstractQueryImpl q = (AbstractQueryImpl)obj;
    this.element.appendChild(q.getElement());
  }

  /**
   * Adds a query object to the list.
   * @param obj     query data object. 
   */
  public boolean add(Object obj) {
    boolean ret = super.add(obj);

    AbstractQueryImpl q = (AbstractQueryImpl)obj;
    this.element.appendChild(q.getElement());
    
    return ret;
  }

  /**
   * Adds query data objects from another collection at a certain index.
   * @param index   insertion point
   * @param c       collection of query data objects. 
   */
  public boolean addAll(int index, Collection c) {
    boolean ret = super.addAll(index,c);
    
    Iterator it = c.iterator();
    while( it.hasNext() ) {
      AbstractQueryImpl q = (AbstractQueryImpl)it.next();
      this.element.appendChild(q.getElement());
    }
    return ret;
  }

  /**
   * Adds query data objects from another collection.
   * @param c       collection of query data objects. 
   */
  public boolean addAll(Collection c) {
    boolean ret = super.addAll(c);
    Iterator it = c.iterator();
    while( it.hasNext() ) {
      AbstractQueryImpl q = (AbstractQueryImpl)it.next();
      this.element.appendChild(q.getElement());
    }
    return ret;
  }

  /**
   * Removes a query data object from this list.
   * @param obj   query data object to remove.
   */
  public boolean remove(Object obj) {
    boolean ret = super.remove(obj);
    if( obj != null ) {
      AbstractQueryImpl q = (AbstractQueryImpl)obj;
      this.element.removeChild(q.getElement());
    }
    return ret;
  }
  
  /**
   * Removes a query data object from this list based on an index.
   * @param index   index of query data object to remove.
   */
  public Object remove(int index) {
    Object obj = super.remove(index);
    if( obj != null ) {
      AbstractQueryImpl q = (AbstractQueryImpl)obj;
      this.element.removeChild(q.getElement());
    }
    return obj;
  }
  
}
